// pages/mine/other/address_creat.js
const App = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userName:'',
    telphone:'',
    address:'', //收货地址
    door:'', //门牌号
    type:'creat',
    listShow:false,
    scrollTop:0,
    tempProvice:{},
    city: {},
    alertList:[],
    editId:'' //编辑地址的id
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.id){
      this.setData({
        type: 'edit',
        editId: options.id
      })
      wx.setNavigationBarTitle({
        title: '编辑地址',
      })
      this.onGetAddress();//获取地址信息
    }else{
      this.setData({
        type: 'creat'
      })
    }
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  // 收货人
  inputUsername:function(e){
    this.setData({
      userName: e.detail.value
    })
  },

  // 绑定手机号
  inputTelPhone:function(e){
    this.setData({
      telphone: e.detail.value
    })
  },

  // 绑定收货地址
  inputAddress:function(e){
    this.setData({
      address: e.detail.value
    })
  },

  // 绑定门牌号
  inputHome: function (e) {
    this.setData({
      door: e.detail.value
    })
  },

  //获取地址信息
  onGetAddress: function () {
    let that = this;
    wx.request({
      url: App.data.globalAPI + '/users/address/get',
      data: {
        id: that.data.editId,
        token: wx.getStorageSync('token')
      },
      header: {
        'content-type': 'application/json' //默认值
      },
      success: function (res) {
        if (res.data.error == 0) {
          wx.hideLoading();
          let arr = res.data.data;
          that.setData({
            userName: arr.user_name,
            telphone: arr.user_phone,
            address: arr.address, //收货地址
            door: arr.door, //门牌号
            tempProvice: { 
              'id': arr.province_id,
              'name': arr.province_name
            },
            city: {
              'id': arr.city_id,
              'name': arr.city_name
            },
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }, fail(res) {
        wx.hideLoading();
        wx.showModal({
          title: '提示',
          content: '网络异常，请稍后重试！',
          showCancel: false,
          success(res) {
          }
        })
      }
    })
  },

  //保存地址
  onSaveAddress: function (id) {
    let that = this;
    if (that.data.userName == '' || that.data.telphone == '' || that.data.city.id == '' || that.data.address == '' || that.data.door==''){
        wx.showToast({
          title: '信息不完整',
          icon:'none'
        })
        return false;
    }
    wx.request({
      url: App.data.globalAPI + '/users/address/save',
      data: {
        token: wx.getStorageSync('token'),
        id: that.data.editId,
        user_name: that.data.userName,
        user_phone: that.data.telphone,
        province_id: that.data.tempProvice.id,
        city_id: that.data.city.id,
        address: that.data.address,
        door: that.data.door
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        if (res.data.error == 0) {
          wx.showToast({
            title: '保存成功',
          })
          let page = getCurrentPages();
          var lastpage = page[page.length - 2];
          lastpage.onLoadAddressList();
          setTimeout(function(){
            wx.navigateBack({
              detal:1
            })
          },600)
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }, fail(res) {
        wx.hideLoading();
        wx.showModal({
          title: '提示',
          content: '网络异常，请稍后重试！',
          showCancel: false,
          success(res) {
          }
        })
      }
    })
  },
  

  //省份选择
  oncityHandle: function () {
    this.setData({
      tempProvice:{},
      city: {},
      listShow:true
    })
    this.loadProviceData('');
  },
 
  //选择信息
  chosedMsg: function (e) {
    let value = e.currentTarget.dataset.value;
    if (Object.keys(this.data.tempProvice).length == 0 && Object.keys(this.data.city).length==0){ //选择的是学校
      this.setData({
        alertList: [],
        scrollTop: 0,
        tempProvice: value
      })
      this.loadProviceData(this.data.tempProvice.id);
    }else{
      this.setData({
        alertList: [],
        scrollTop: 0,
        city: value,
        listShow:false
      })
    }
  },
  //省份搜索
  loadProviceData: function (pId) {
    let that = this;
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: App.data.globalAPI + '/search/index/city',
      data: {
        token: wx.getStorageSync('token'),
        pid: pId,
        name: ''
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        const arr = res.data;
        if (arr.error == 0) {
          that.setData({
            alertList: arr.data
          })
        } else {
          wx.showToast({
            title: arr.msg,
            icon: 'none',
            duration: 800
          })
        }
      }, complete() {
        wx.hideLoading();
      }
    })
  },

  onHideAlert: function () {
    this.setData({
      listShow: false
    })
  },

  //删除当前地址
  deleteAddress: function () {
    let that = this;
    wx.request({
      url: App.data.globalAPI + '/users/address/del',
      data: {
        id: that.data.editId,
        token: wx.getStorageSync('token')
      },
      header: {
        'content-type': 'application/json' //默认值
      },
      success: function (res) {
        if (res.data.error == 0) {
          wx.showToast({
            icon:'none',
            title: '删除成功'
          })
          let page = getCurrentPages();
          var lastpage = page[page.length - 2];
          lastpage.onLoadAddressList();
          setTimeout(function () {
            wx.navigateBack({
              detal: 1
            })
          }, 600)
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }, fail(res) {
        wx.hideLoading();
        wx.showModal({
          title: '提示',
          content: '网络异常，请稍后重试！',
          showCancel: false,
          success(res) {
          }
        })
      }
    })
  },
})